﻿<?php
	
	use Workerman\Worker;
	use Workerman\Mysql;
	use \Workerman\Lib\Timer;
	use \Workerman\Protocols\Http;
	
	require_once IA_ROOT . "/../Libs/class/debug.class.php";
	require_once IA_ROOT . "/../Libs/function/comm.func.php";
	
    //** 实例化 Trigger\Server类，开启一个基于frame协议的worker,端口为3209
	$g_trigger_server = new Trigger\Server('0.0.0.0', TRIGGER_PORT, 5);
	$g_trigger_server->onTrigger("all", function($data){
		// var_dump(123);
	});
	
	$g_trigger_server->onTrigger("set_process_result", function($data){
			

		//var_dump( $data );
		/*
		$trigger_mes_obj = (object)null;
		//如果为-1，则是最后一个工序
		$trigger_mes_obj->batch = $batch;
		$trigger_mes_obj->unit_batch = $unit_batch;
		$trigger_mes_obj->next_process_code = $next_process_code;
		$trigger_mes_obj->process_code = $process_code;
		$trigger_mes_obj->workingclass  = $workingclass;
		$trigger_mes_obj->workingsubclass = $workingsubclass;
		$trigger_mes_obj->tray = $tray;
		$trigger_mes_obj->lot = $lot;
		$trigger_mes_obj->date = date('Y-m-d H:i:s'); 
		$trigger_mes_obj->type = $type;
		$trigger_mes_obj->finish_battery_count = $good_battery_count;
		if(empty($param['DEVICE_CODE']))
			$device_code = "";
		else
			$device_code = $param['DEVICE_CODE'];
		$trigger_mes_obj->device_code = $device_code;
		
		$battery_ids = array();
		for($i=0; $i<count($ret_battery); $i++)
			array_push($battery_ids, $ret_battery[$i]['battery_id']);
		$trigger_mes_obj->battery_ids = $battery_ids;
		*/
        $workingclass = $data['workingclass'];
        $device_code = $data['device_code'];
        if($workingclass == "DV" || $workingclass == "OCVR" || $workingclass == "FORMATION"){
            $batch = $data['batch'];
            $sql = "SELECT b.code, c.code as process_category_code  FROM hf_mes_production_planning_management_batch a INNER JOIN hf_mes_technology_flow b ON a.flow_id = b.id INNER JOIN hf_mes_flow_process_category c ON b.process_category_id = c.id WHERE a.batch='$batch' LIMIT 1";
            $ret = sql_query($sql);
            // 判断如果是自动校准流程，则把hf_mes_device表中的auto_date更新为当前时间、manual_calibration改成0
            $process_category_code = $ret[0]['process_category_code'];
            if($process_category_code  == "AUTO"){
                $sql = "UPDATE hf_mes_device SET auto_date=CURRENT_TIMESTAMP,manual_calibration=0 WHERE code='$device_code'";
                sql_query($sql);
            }
        }
        
		
		if($data['next_process_code']==-1 && $data['finish_battery_count']>0){
			//Process为完成的
			$batch = $data['batch'];
			$unit_batch = $data['unit_batch'];
			$lot = $data['lot'];
			$tray = $data['tray'];
			$finish_battery_count = $data['finish_battery_count'];
			$sql = "INSERT INTO `hf_mes`.`hf_mes_batch_daily` (`batch`, `unit_batch`, `lot`, `tray`, `finish_battery_count`) 
			              VALUES ('$batch', '$unit_batch', '$lot', '$tray', '$finish_battery_count');";
			sql_query($sql);
		}
		
		//响应set_process_result接口的抛出
		
		//print( $data['uname'] );
	});
	
	
	
	// 如果不是在根目录启动，则运行runAll方法
	if(!defined('GLOBAL_START')){
		Worker::runAll();
	}
	
	
?>